package com.allen.study.application.service;

import com.allen.study.application.api.assembler.DepartmentDTOAssembler;
import com.allen.study.application.api.request.DepartmentCreateRequest;
import com.allen.study.application.api.request.DepartmentQueryRequest;
import com.allen.study.application.api.request.DepartmentUpdateRequest;
import com.allen.study.application.api.response.DepartmentQueryResponse;
import com.allen.study.application.api.response.DepartmentResponse;
import com.allen.study.application.repository.IDepartmentReadModelRepo;
import com.allen.study.domain.entity.Department;
import com.allen.study.domain.service.DepartmentService;
import com.allen.study.common.base.ApiPageResponse;
import com.allen.study.common.base.ApiResponse;
import com.allen.study.common.base.DomainResponse;
import com.allen.study.common.base.Pagination;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 部门信息表应用服务
 *
 * @author AllenSun
 * @since 2025-04-23 20:46
 */
@Service
@AllArgsConstructor
public class DepartmentAppService {

    /**
     * 部门信息表领域服务
     */
    private final DepartmentService departmentService;

    /**
     * 部门信息表读模型资源库
     */
    private final IDepartmentReadModelRepo departmentReadModelRepo;

    /**
     * 部门信息表数据传输对象转换器
     */
    private final DepartmentDTOAssembler departmentDTOAssembler;

    /**
     * 创建部门信息表
     *
     * @param createRequest 部门信息表创建对象
     * @return 响应结果
     */
    public ApiResponse<Void> create(DepartmentCreateRequest createRequest) {
        // 部门信息表创建对象 转成 部门信息表实体
        Department department = departmentDTOAssembler.assembler(createRequest);
        // 调用创建部门信息表领域服务
        DomainResponse<Void> domainResponse = departmentService.create(department);
        // 领域响应结果转成API响应结果
        return ApiResponse.valueOf(domainResponse);
    }

    /**
     * 根据主键删除部门信息表
     *
     * @param departmentId 部门信息表主键
     * @return 响应结果
     */
    public ApiResponse<Void> deleteById(String departmentId) {
        // 调用删除部门信息表领域服务
        DomainResponse<Void> domainResponse = departmentService.deleteById(departmentId);
        // 领域响应结果转成API响应结果
        return ApiResponse.valueOf(domainResponse);
    }

    /**
     * 根据主键更新部门信息表
     *
     * @param departmentId 部门信息表主键
     * @param updateRequest 部门信息表更新对象
     * @return 响应结果
     */
    public ApiResponse<Void> updateById(String departmentId, DepartmentUpdateRequest updateRequest) {
        // 部门信息表更新对象 转成 部门信息表实体
        Department department = departmentDTOAssembler.assembler(updateRequest);
        // 设置主键
        department.setId(departmentId);
        // 调用更新部门信息表领域服务
        DomainResponse<Void> domainResponse = departmentService.updateById(department);
        // 领域响应结果转成API响应结果
        return ApiResponse.valueOf(domainResponse);
    }

    /**
     * 根据主键查询部门信息表详情
     *
     * @param departmentId 部门信息表主键
     * @return 响应结果
     */
    public ApiResponse<DepartmentResponse> queryById(String departmentId) {
        // 根据主键查询部门信息表
        DomainResponse<Department> domainResponse = departmentService.queryById(departmentId);
        // 领域响应结果转成API响应结果
        return ApiResponse.valueOf(domainResponse, departmentDTOAssembler::toResponse);
    }

    /**
     * 分页查询部门信息表
     *
     * @param pagination   分页
     * @param queryRequest 部门信息表查询对象
     * @return 响应结果
     */
    public ApiPageResponse<DepartmentQueryResponse> query(Pagination pagination, DepartmentQueryRequest queryRequest) {
        // 调用部门信息表读模型资源库分页查询
        List<DepartmentQueryResponse> list = departmentReadModelRepo.query(pagination, queryRequest);
        return ApiPageResponse.ok(pagination, list);
    }
}
